package com.liuzhongni.wahahanews.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.os.Handler;
import android.os.Message;
import android.support.v4.view.PagerAdapter;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.liuzhongni.wahahanews.R;
import com.liuzhongni.wahahanews.headlineclass.NewHeadlinePhotoDatails;
import com.liuzhongni.wahahanews.headlineclass.special.DownloadImage;

import java.util.ArrayList;

import de.greenrobot.event.EventBus;

/**
 * Created by dllo on 16/1/15.
 */
public class ViewPagerAdapterOfNewPhotoDatails extends PagerAdapter {
    private ArrayList<NewHeadlinePhotoDatails> datailses;
    private ArrayList<View> data;
    private Context context;
    private ImageView imageView;
    private ImageView[] imageViews;
    private RelativeLayout relativeLayout;
    private DownloadImage downloadImage;
    private EventBus eventBus;
    private Handler handler;

    public ArrayList<NewHeadlinePhotoDatails> getDatailses() {
        return datailses;
    }

    public ViewPagerAdapterOfNewPhotoDatails(Context context, ArrayList<NewHeadlinePhotoDatails> datailses, RelativeLayout relativeLayout) {
        this.context = context;
        this.datailses = datailses;
        this.relativeLayout = relativeLayout;
        init();
    }

    private void init() {
        data = new ArrayList<>();
        WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        int reqWidth = wm.getDefaultDisplay().getWidth();
        int reqHeight = 300;


        imageViews = new ImageView[datailses.size()];
        for (int i = 0; i < datailses.size(); i++) {
            Log.d("ViewPagerAdapterOfNewPh", "1:" + 1);
            final Bitmap[] bitmap = new Bitmap[1];
            final int finalI = i;
            View view = LayoutInflater.from(context).inflate(R.layout.new_headline_photodatails_item, null);

            imageView = (ImageView) view.findViewById(R.id.photodatails_item_iv);
            imageViews[i] = imageView;
            handler = new Handler(new Handler.Callback() {
                @Override
                public boolean handleMessage(Message msg) {
                    if (msg.what == 100) {
                        bitmap[0] = (Bitmap) msg.obj;
                        imageViews[finalI].setImageBitmap(bitmap[0]);

                    }
                    return false;
                }
            });
            downloadImage = (DownloadImage) new DownloadImage(handler).execute(datailses.get(i).getImgurl());
            Log.d("ViewPagerAdapterOfNewPh", datailses.get(i).getImgurl());
            Log.d("ViewPagerAdapterOfNewPh", "i:" + i);
            Log.d("ViewPagerAdapterOfNewPh", "2:" + 2);
            data.add(view);
//            VolleySingleton.getOurInstance().imgJson(imageView,datailses.get(i).getImgurl());

        }
    }
//    @Subscribe()
//    public void onEvent(Bitmap bitmap){
//
//        for (int i = 0; i < datailses.size(); i++) {
//            downloadImage = (DownloadImage) new DownloadImage().execute(datailses.get(i).getImgurl());
//            imageViews[i].setImageBitmap(bitmap);
//        }
//    }

    @Override
    public int getCount() {
        return datailses.size();
    }


    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

    @Override
    public Object instantiateItem(ViewGroup container, final int position) {
        View view = data.get(position);
        Log.d("ViewPagerAdapterOfNewPh", "wahaha");
        imageViews[position].setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {


                if (relativeLayout.getVisibility() == View.INVISIBLE) {
                    relativeLayout.setVisibility(View.VISIBLE);
                } else {
                    relativeLayout.setVisibility(View.INVISIBLE);
                }
            }
        });
        container.addView(view);
        return view;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        View view = data.get(position);
        container.removeView(view);
    }
}
